package tsing.javase.functionProgram.stream;



import com.alibaba.fastjson.JSON;
import tsing.javase.functionProgram.bean.Person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author Tsing/吴清平
 * @Date 2022/12/29 17:19
 * @Version 1.0
 */
public class SteamDemo {

    public static void main(String[] args) {

//        method1();

        //map
//        method2();

        //去重 distinct
//        method3();



        //排序sorted
//        method4();

        //flapmap :
        method5();


        //并行流
        method6();
    }

    private static void method6() {

        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        //串行流求和
/*        Integer integer = stream.filter(num -> num > 5).reduce(new BinaryOperator<Integer>() {

            @Override
            public Integer apply(Integer result, Integer ele) {
                return result + ele;
            }
        }).get();*/

        //求和方式2
/*        Integer integer = stream.filter(num -> num > 5).reduce((x, y) -> x + y).get();
        System.out.println(integer);*/

        //求和方式3
      /*  Integer integer1 = stream.filter(num -> num > 5).reduce(Integer::sum).get();
        System.out.println(integer1);*/

        //并行流 :求乘积
    /*    Integer integer = stream.parallel()  // parallel():可以将串行流改成并行流
                .filter(num -> num > 5)
                .reduce((x, y) -> x * y)  //求乘积
                .get();
        System.out.println(integer);*/

        Integer integer1 = stream.parallel()
                .peek(num -> System.out.println(Thread.currentThread().getName() + ":\t"+num))  //peek可以看到并行流中各线程的操作，类似于debug
                .reduce((x, y) -> (x - y) > 0 ? x:y).get(); //取最值
        System.out.println(integer1);


    }

    private static void method5() {
        List<String> list = Arrays.asList("m|k|l|a", "1|3|5|7");

        List<String> listNew = list.stream().flatMap(s -> {
            // 将每个元素转换成一个stream
            String[] split = s.split("||");
            Stream<String> s2 = Arrays.stream(split).filter(str-> !str.equals("|"));
            return s2;
        }).collect(Collectors.toList());

        System.out.println("处理前的集合：" + list);
        System.out.println("处理后的集合：" + listNew);
    }

    private static void method4() {
        List<Person> list = getList();
       /* list.stream().sorted(new Comparator<Person>(){
            @Override
            public int compare(Person o1, Person o2) {
                return (o1.getAge()-o2.getAge())>0?1:-1;
            }
        }).forEach(person -> {
            System.out.println(JSON.toJSONString(person));
        });
*/


        list.stream().sorted((person1,person2) ->{
            return (person2.getAge()-person1.getAge())>0?-1:1;  // 后一个 是否大于前一个，是的，返回-1,不换位置；不是，返回1：换位置  ->升序
        }).forEach(person -> System.out.println(JSON.toJSONString(person)));



    }


    //例子1：得到一个年龄> 21 的 新集合
    public static void method1(){

        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 24, "female", "New York"));
        personList.add(new Person("Owen", 9500, 25, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 26, "female", "New York"));

        //例子1：得到一个年龄> 21 的新集合
        /*List<Person> collect = personList.stream()//把集合转换成流
                .distinct()
                .filter(new Predicate<Person>() {
                    @Override
                    public boolean test(Person o) {
                        return o.getAge() > 21;
                    }
                }) //筛选出年龄大于21的
                .collect(Collectors.toList());*/

        //改造成lamda表达式
       /* List<Person> collect = personList.stream()//把集合转换成流
                .distinct()
                .filter(o -> o.getAge()>21) //筛选出年龄大于21的
                .collect(Collectors.toList());
*/






    }


    private static void method2() {

        List<Person> list = getList();
        //例子2：map使用,得到一个名字的List集合
    /*    list.stream()
                .map(new Function<Person,String>(){
                    @Override
                    public String apply(Person o) {
                        return o.getName();
                    }
                } )
                .forEach(new Consumer<String>(){
                    @Override
                    public void accept(String o) {
                        System.out.println(o);
                    }
                });*/


        //转为lamda格式
        list.stream().map(person -> person.getName()).forEach((s) ->{ System.out.println(s);});


        //例子3：map使用,将person的年龄+5

        list.stream().map(Person::getAge).map(age -> age+5).forEach(age -> System.out.println(age));


    }


    private static void method3() {
        List<Person> list = getList();
        list.stream().distinct().forEach(person -> {
            System.out.println(JSON.toJSONString(person));
        });

    }


    public static List<Person>getList(){
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 24, "female", "New York"));
        personList.add(new Person("Owen", 9500, 25, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
        personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
        return personList;
    }
}

